<?php
// 本类由系统自动生成，仅供测试用途
namespace Home\Controller;
use Think\Controller;


class CompetitionController extends Controller {
    //全局变量区
    /**
     * 布局相关
     * list_each_page_number：每一页列表条数
     */
    public $list_each_page_number = 5;

    private $second_first_clash = array(
        38 => 1,
        1 => 2,
        2 => 5,
        3 => 6,
        4 => 9,
        5 => 10,
        6 => 13,
        7 => 14,
        8 => 17,
        9 => 18,
        10 => 21,
        11 => 22,
        12 => 33,
        13 => 34,
        14 => 35,
        15 => 36,
        16 => 37,
        17 => 38,
        18 => 51,
        19 => 52,
        20 => 55,
        21 => 56,
        22 => 59,
        23 => 60,
        24 => 63,
        25 => 64,
        26 => 75,
        27 => 76,
        28 => 77,
        29 => 78,
        30 => 79,
        31 => 80,
        32 => 81,
        33 => 82,
        34 => 83,
        35 => 84,
        36 => 85,
        37 => 86
        );

    private $second_second_clash = array(
        1 => 11,
        2 => 12,
        3 => 15,
        4 => 16,
        5 => 19,
        6 => 20,
        7 => 23,
        8 => 24,
        9 => 39,
        10 => 40,
        11 => 41,
        12 => 42,
        13 => 43,
        14 => 44,
        15 => 45,
        16 => 46,
        17 => 47,
        18 => 48,
        19 => 49,
        20 => 50,
        21 => 53,
        22 => 54,
        23 => 57,
        24 => 58,
        25 => 61,
        26 => 62,
        27 => 65,
        28 => 66
        );

    private $second_first_position = array (
        1 =>'c_a1',
        2 =>'c_b2',
        3 =>'d_a2',
        4 =>'d_b1',
        5 =>'d_a1',
        6 =>'d_b3',
        7 =>'d_c1',
        8 =>'e_a2',
        9 =>'e_b2',
        10 =>'e_c2',
        11 =>'e1_a1',
        12 =>'e1_c2',
        13 =>'e2_c1',
        14 =>'e2_a2',
        15 =>'f1_b1',
        16 =>'f1_d2',
        17 =>'f2_d1',
        18 =>'f2_b2',
        19 =>'g1_a1',
        20 =>'g1_d1',
        21 =>'g2_b1',
        22 =>'g2_e1',
        23 =>'g3_c1',
        24 =>'g3_f1',
        25 =>'j1_a1',
        26 =>'j1_e1',
        27 =>'j2_c1',
        28 =>'j2_g1',
        29 =>'j3_b1',
        30 =>'j3_f1',
        31 =>'j4_d1',
        32 =>'j4_h1'
        );

    private $second_second_position = array (                  
        1 => 'h1_e1' ,
        2 => 'h1_e2' ,       
        3 => 'h2_f1' ,
        4 => 'h2_f2' ,       
        5 => 'j1_e3' ,
        6 => 'j1_e2' ,      
        7 => 'j2_f1' ,
        8 => 'j2_f2' ,               
        9 => 'gs_g1' ,
        10 => 'gf_g4' ,        
        11 => 'gs_g2' ,
        12 => 'gf_g5' ,        
        13 => 'gs_g3' ,
        14 => 'gf_g6' ,       
        15 => 'k1_j1' ,
        16 => 'k1_j2' ,      
        17 => 'k2_j3' ,
        18 => 'k2_j4' ,       
        19 => 'l1_j1' ,
        20 => 'l1_j2' ,       
        21 => 'l2_j3' ,
        22 => 'l2_j4'
        );
    
    private $group_num_transform = array(
        0 => 0,
        1 => 0,
        2 => 0,
        3 => 0,
        4 => 0,
        5 => 2,
        6 => 2,
        7 => 2,
        8 => 2,
        9 => 3,
        10 => 4,
        11 => 4,
        12 => 4,
        13 => 4,
        14 => 4,
        15 => 4,
        16 => 4,
        17 => 6,
        18 => 6,
        19 => 8,
        20 => 8,
        21 => 8,
        22 => 8,
        23 => 8,
        24 => 8,
        );

    private $clash_transform = array(
        'f_a1' => 1,
        'f_a2' => 2,
        'f_a3' => 3,
        'f_a4' => 4,
        'f_b1' => 5,
        'f_b2' => 6,
        'f_b3' => 7,
        'f_b4' => 8,
        'f_c1' => 9,
        'f_c2' => 10,
        'f_c3' => 11,
        'f_c4' => 12,
        'f_d1' => 13,
        'f_d2' => 14,
        'f_d3' => 15,
        'f_d4' => 16,
        'f_e1' => 17,
        'f_e2' => 18,
        'f_e3' => 19,
        'f_f1' => 20,
        'f_f2' => 21,
        'f_f3' => 22,
        'f_g1' => 23,
        'f_g2' => 24,
        'f_g3' => 25,
        'f_h1' => 26,
        'f_h2' => 27,
        'f_h3' => 28
        );

    private $clash_second_transform = array(
        'c_a1' => 1,
        'c_b2' => 2,
        'e_cs' => 3,
        'e_ds' => 4,
        'd_a2' => 5,
        'd_b1' => 6,
        'f_cf' => 7,
        'f_df' => 8,
        'e1_a1' => 9,
        'e1_c2' => 10,
        'h1_e1' => 11,
        'h1_e2' => 12,
        'e2_c1' => 13,
        'e2_a2' => 14,
        'h2_f1' => 15,
        'h2_f2' => 16,
        'f1_b1' => 17,
        'f1_d2' => 18,
        'j1_e3' => 19,
        'j1_e2' => 20,
        'f2_d1' => 21,
        'f2_b2' => 22,
        'j2_f1' => 23,
        'j2_f2' => 24,
        'h1s' => 25,
        'h2s' => 26,
        'h1f' => 27,
        'h2f' => 28,
        'j1s' => 29,
        'j2s' => 30,
        'j1f' => 31,
        'j2f' => 32,
        'g1_a1' => 33,
        'g1_d1' => 34,
        'gs_g1' => 35,
        'gf_g4' => 36,
        'g2_b1' => 37,
        'g2_e1' => 38,
        'gs_g2' => 39,
        'gf_g5' => 40,
        'g3_c1' => 41,
        'g3_f1' => 42,
        'gs_g3' => 43,
        'gf_g6' => 44,
        'j1_a1' => 45,
        'j1_e1' => 46,
        'k1_j1' => 47,
        'k1_j2' => 48,
        'j2_c1' => 49,
        'j2_g1' => 50,
        'k2_j3' => 51,
        'k2_j4' => 52,
        'j3_b1' => 53,
        'j3_f1' => 54,
        'l1_j1' => 55,
        'l1_j2' => 56,
        'j4_d1' => 57,
        'j4_h1' => 58,
        'l2_j3' => 59,
        'l2_j4' => 60,
        'k1s' => 61,
        'k2s' => 62,
        'k1f' => 63,
        'k2f' => 64,
        'l1s' => 65,
        'l2s' => 66,
        'l1f' => 67,
        'l2f' => 68,
        'd_a1' => 69,
        'd_b3' => 70,
        'd_c1' => 71,
        'e_a2' => 72,
        'e_b2' => 73,
        'e_c2' => 74
        );

    private $tr_clash_second_transform = array(
        1 => 'c_a1' ,
        2 => 'c_b2' ,
        3 => 'e_cs' ,
        4 => 'e_ds' ,
        5 => 'd_a2' ,
        6 => 'd_b1' ,
        7 => 'f_cf' ,
        8 => 'f_df' ,
        9 => 'e1_a1' ,
        10 => 'e1_c2' ,
        11 => 'h1_e1' ,
        12=> 'h1_e2' ,
        13 => 'e2_c1' ,
        14 => 'e2_a2' ,
        15 => 'h2_f1' ,
        16 => 'h2_f2' ,
        17 => 'f1_b1' ,
        18 => 'f1_d2' ,
        19 => 'j1_e3' ,
        20 => 'j1_e2' ,
        21 => 'f2_d1' ,
        22 => 'f2_b2' ,
        23 => 'j2_f1' ,
        24 => 'j2_f2' ,
        25 => 'h1s' ,
        26 => 'h2s' ,
        27 => 'h1f' ,
        28 => 'h2f' ,
        29 => 'j1s' ,
        30 => 'j2s' ,
        31 => 'j1f' ,
        32 => 'j2f' ,
        33 => 'g1_a1' ,
        34 => 'g1_d1' ,
        35 => 'gs_g1' ,
        36 => 'gf_g4' ,
        37 => 'g2_b1' ,
        38 => 'g2_e1' ,
        39 => 'gs_g2' ,
        40 => 'gf_g5' ,
        41 => 'g3_c1' ,
        42 => 'g3_f1' ,
        43 => 'gs_g3' ,
        44 => 'gf_g6' ,
        45 => 'j1_a1' ,
        46 => 'j1_e1' ,
        47 => 'k1_j1' ,
        48 => 'k1_j2' ,
        49 => 'j2_c1' ,
        50 => 'j2_g1' ,
        51 => 'k2_j3' ,
        52 => 'k2_j4' ,
        53 => 'j3_b1' ,
        54 => 'j3_f1' ,
        55 => 'l1_j1' ,
        56 => 'l1_j2' ,
        57 => 'j4_d1' ,
        58 => 'j4_h1' ,
        59 => 'l2_j3' ,
        60 => 'l2_j4' ,
        61 => 'k1s' ,
        62 => 'k2s' ,
        63 => 'k1f' ,
        64 => 'k2f' ,
        65 => 'l1s' ,
        66 => 'l2s' ,
        67 => 'l1f' ,
        68 => 'l2f' ,
        69 => 'd_a1' ,
        70 => 'd_b3' ,
        71 => 'd_c1' ,
        72 => 'e_a2' ,
        73 => 'e_b2' ,
        74 => 'e_c2' 
        );

    private $t_clash_transform = array(
         1 => 'f_a1' ,
         2 => 'f_a2' ,
         3 => 'f_a3' ,
         4 => 'f_a4' ,
         5 => 'f_b1' ,
         6 => 'f_b2' ,
         7 => 'f_b3' ,
         8 => 'f_b4' ,
         9 => 'f_c1' ,
         10 => 'f_c2' ,
         11 => 'f_c3' ,
         12 => 'f_c4' ,
         13 => 'f_d1' ,
         14 => 'f_d2' ,
         15 => 'f_d3' ,
         16 => 'f_d4' ,
         17 => 'f_e1' ,
         18 => 'f_e2' ,
         19 => 'f_e3' ,
         20 => 'f_f1' ,
         21 => 'f_f2' ,
         22 => 'f_f3' ,
         23 => 'f_g1' ,
         24 => 'f_g2' ,
         25 => 'f_g3' ,
         26 => 'f_h1' ,
         27 => 'f_h2' ,
         28 => 'f_h3' 
        );

    private $score_mode_transform = array(
        'f_a1_a2' => 1,
        'f_a1_a3' => 2,
        'f_a1_a4' => 3,
        'f_a2_a1' => 4,
        'f_a2_a3' => 5,
        'f_a2_a4' => 6,
        'f_a3_a1' => 7,
        'f_a3_a2' => 8,
        'f_a3_a4' => 9,
        'f_a4_a1' => 10,
        'f_a4_a2' => 11,
        'f_a4_a3' => 12,
        'f_b1_b2' => 13,
        'f_b1_b3' => 14,
        'f_b1_b4' => 15,
        'f_b2_b1' => 16,
        'f_b2_b3' => 17,
        'f_b2_b4' => 18,
        'f_b3_b1' => 19,
        'f_b3_b2' => 20,
        'f_b3_b4' => 21,
        'f_b4_b1' => 22,
        'f_b4_b2' => 23,
        'f_b4_b3' => 24,
        'f_c1_c2' => 25,
        'f_c1_c3' => 26,
        'f_c1_c4' => 27,
        'f_c2_c1' => 28,
        'f_c2_c3' => 29,
        'f_c2_c4' => 30,
        'f_c3_c1' => 31,
        'f_c3_c2' => 32,
        'f_c3_c4' => 33,
        'f_c4_c1' => 34,
        'f_c4_c2' => 35,
        'f_c4_c3' => 36,
        'f_d1_d2' => 37,
        'f_d1_d3' => 38,
        'f_d1_d4' => 39,
        'f_d2_d1' => 40,
        'f_d2_d3' => 41,
        'f_d2_d4' => 42,
        'f_d3_d1' => 43,
        'f_d3_d2' => 44,
        'f_d3_d4' => 45,
        'f_d4_d1' => 46,
        'f_d4_d2' => 47,
        'f_d4_d3' => 48,
        'f_e1_e2' => 49,
        'f_e1_e3' => 50,
        'f_e2_e1' => 51,
        'f_e2_e3' => 52,
        'f_e3_e1' => 53,
        'f_e3_e2' => 54,
        'f_f1_f2' => 55,
        'f_f1_f3' => 56,
        'f_f2_f1' => 57,
        'f_f2_f3' => 58,
        'f_f3_f1' => 59,
        'f_f3_f2' => 60,
        'f_g1_g2' => 61,
        'f_g1_g3' => 62,
        'f_g2_g1' => 63,
        'f_g2_g3' => 64,
        'f_g3_g1' => 65,
        'f_g3_g2' => 66,
        'f_h1_h2' => 67,
        'f_h1_h3' => 68,
        'f_h2_h1' => 69,
        'f_h2_h3' => 70,
        'f_h3_h1' => 71,
        'f_h3_h2' => 72
        );

    private $tr_score_mode_transform = array(
        1 => 'f_a1_a2' ,
        2 => 'f_a1_a3' ,
        3 => 'f_a1_a4' ,
        4 => 'f_a2_a1' ,
        5 => 'f_a2_a3' ,
        6 => 'f_a2_a4' ,
        7 => 'f_a3_a1' ,
        8 => 'f_a3_a2' ,
        9 => 'f_a3_a4' ,
        10 => 'f_a4_a1' ,
        11 => 'f_a4_a2' ,
        12 => 'f_a4_a3' ,
        13 => 'f_b1_b2' ,
        14 => 'f_b1_b3' ,
        15 => 'f_b1_b4' ,
        16 => 'f_b2_b1' ,
        17 => 'f_b2_b3' ,
        18 => 'f_b2_b4' ,
        19 => 'f_b3_b1' ,
        20 => 'f_b3_b2' ,
        21 => 'f_b3_b4' ,
        22 => 'f_b4_b1' ,
        23 => 'f_b4_b2' ,
        24 => 'f_b4_b3' ,
        25 => 'f_c1_c2' ,
        26 => 'f_c1_c3' ,
        27 => 'f_c1_c4' ,
        28 => 'f_c2_c1' ,
        29 => 'f_c2_c3' ,
        30 => 'f_c2_c4' ,
        31 => 'f_c3_c1' ,
        32 => 'f_c3_c2' ,
        33 => 'f_c3_c4' ,
        34 => 'f_c4_c1' ,
        35 => 'f_c4_c2' ,
        36 => 'f_c4_c3' ,
        37 => 'f_d1_d2' ,
        38 => 'f_d1_d3' ,
        39 => 'f_d1_d4' ,
        40 => 'f_d2_d1' ,
        41 => 'f_d2_d3' ,
        42 => 'f_d2_d4' ,
        43 => 'f_d3_d1' ,
        44 => 'f_d3_d2' ,
        45 => 'f_d3_d4' ,
        46 => 'f_d4_d1' ,
        47 => 'f_d4_d2' ,
        48 => 'f_d4_d3' ,
        49 => 'f_e1_e2' ,
        50 => 'f_e1_e3' ,
        51 => 'f_e2_e1' ,
        52 => 'f_e2_e3' ,
        53 => 'f_e3_e1' ,
        54 => 'f_e3_e2' ,
        55 => 'f_f1_f2' ,
        56 => 'f_f1_f3' ,
        57 => 'f_f2_f1' ,
        58 => 'f_f2_f3' ,
        59 => 'f_f3_f1' ,
        60 => 'f_f3_f2' ,
        61 => 'f_g1_g2' ,
        62 => 'f_g1_g3' ,
        63 => 'f_g2_g1' ,
        64 => 'f_g2_g3' ,
        65 => 'f_g3_g1' ,
        66 => 'f_g3_g2' ,
        67 => 'f_h1_h2' ,
        68 => 'f_h1_h3' ,
        69 => 'f_h2_h1' ,
        70 => 'f_h2_h3' ,
        71 => 'f_h3_h1' ,
        72 => 'f_h3_h2'
        );

    private $score_second_mode_transform = array(
        's_c_a1_c_b2' => 1,
        's_c_b2_c_a1' => 2,
        's_e_cs_e_ds' => 3,
        's_e_ds_e_cs' => 4,
        's_d_a2_d_b1' => 5,
        's_d_b1_d_a2' => 6,
        's_f_cf_f_df' => 7,
        's_f_df_f_cf' => 8,
        's_e1_a1_e1_c2' => 9,
        's_e1_c2_e1_a1' => 10,
        's_h1_e1_h1_e2' => 11,
        's_h1_e2_h1_e1' => 12,
        's_e2_c1_e2_a2' => 13,
        's_e2_a2_e2_c1' => 14,
        's_h2_f1_h2_f2' => 15,
        's_h2_f2_h2_f1' => 16,
        's_f1_b1_f1_d2' => 17,
        's_f1_d2_f1_b1' => 18,
        's_j1_e3_j1_e2' => 19,
        's_j1_e2_j1_e3' => 20,
        's_f2_d1_f2_b2' => 21,
        's_f2_b2_f2_d1' => 22,
        's_j2_f1_j2_f2' => 23,
        's_j2_f2_j2_f1' => 24,
        's_h1s_h2s' => 25,
        's_h2s_h1s' => 26,
        's_h1f_h2f' => 27,
        's_h2f_h1f' => 28,
        's_j1s_j2s' => 29,
        's_j2s_j1s' => 30,
        's_j1f_j2f' => 31,
        's_j2f_j1f' => 32,
        's_g1_a1_g1_d1' => 33,
        's_g1_d1_g1_a1' => 34,
        's_g2_b1_g2_e1' => 35,
        's_g2_e1_g2_b1' => 36,
        's_g3_c1_g3_f1' => 37,
        's_g3_f1_g3_c1' => 38,
        's_gs_g1_gs_g2' => 39,
        's_gs_g2_gs_g1' => 40,
        's_gs_g1_gs_g3' => 41,
        's_gs_g3_gs_g1' => 42,
        's_gs_g2_gs_g3' => 43,
        's_gs_g3_gs_g2' => 44,
        's_gf_g4_gf_g5' => 45,
        's_gf_g5_gf_g4' => 46,
        's_gf_g4_gf_g6' => 47,
        's_gf_g6_gf_g4' => 48,
        's_gf_g5_gf_g6' => 49,
        's_gf_g6_gf_g5' => 50,
        's_j1_a1_j1_e1' => 51,
        's_j1_e1_j1_a1' => 52,
        's_k1_j1_k1_j2' => 53,
        's_k1_j2_k1_j1' => 54,
        's_j2_c1_j2_g1' => 55,
        's_j2_g1_j2_c1' => 56,
        's_k2_j3_k2_j4' => 57,
        's_k2_j4_k2_j3' => 58,
        's_j3_b1_j3_f1' => 59,
        's_j3_f1_j3_b1' => 60,
        's_l1_j1_l1_j2' => 61,
        's_l1_j2_l1_j1' => 62,
        's_j4_d1_j4_h1' => 63,
        's_j4_h1_j4_d1' => 64,
        's_l2_j3_l2_j4' => 65,
        's_l2_j4_l2_j3' => 66,
        's_k1s_k2s' => 67,
        's_k2s_k1s' => 68,
        's_k1f_k2f' => 69,
        's_k2f_k1f' => 70,
        's_l1s_l2s' => 71,
        's_l2s_l1s' => 72,
        's_l1f_l2f' => 73,
        's_l2f_l1f' => 74,
        's_d_a1_d_b3' => 75,
        's_d_b3_d_a1' => 76,
        's_d_a1_d_c1' => 77,
        's_d_c1_d_a1' => 78,
        's_d_b3_d_c1' => 79,
        's_d_c1_d_b3' => 80,
        's_e_a2_e_b2' => 81,
        's_e_b2_e_a2' => 82,
        's_e_a2_e_c2' => 83,
        's_e_c2_e_a2' => 84,
        's_e_b2_e_c2' => 85,
        's_e_c2_e_b2' => 86      
        );

    private $tr_score_second_mode_transform = array(
        1 => 's_c_a1_c_b2' ,
        2 => 's_c_b2_c_a1' ,
        3 => 's_e_cs_e_ds' ,
        4 => 's_e_ds_e_cs' ,
        5 => 's_d_a2_d_b1' ,
        6 => 's_d_b1_d_a2' ,
        7 => 's_f_cf_f_df' ,
        8 => 's_f_df_f_cf' ,
        9 => 's_e1_a1_e1_c2' ,
        10 => 's_e1_c2_e1_a1' ,
        11 => 's_h1_e1_h1_e2' ,
        12 => 's_h1_e2_h1_e1' ,
        13 => 's_e2_c1_e2_a2' ,
        14 => 's_e2_a2_e2_c1' ,
        15 => 's_h2_f1_h2_f2' ,
        16 => 's_h2_f2_h2_f1' ,
        17 => 's_f1_b1_f1_d2' ,
        18 => 's_f1_d2_f1_b1' ,
        19 => 's_j1_e3_j1_e2' ,
        20 => 's_j1_e2_j1_e3' ,
        21 => 's_f2_d1_f2_b2' ,
        22 => 's_f2_b2_f2_d1' ,
        23 => 's_j2_f1_j2_f2' ,
        24 => 's_j2_f2_j2_f1' ,
        25 => 's_h1s_h2s' ,
        26 => 's_h2s_h1s' ,
        27 => 's_h1f_h2f' ,
        28 => 's_h2f_h1f' ,
        29 => 's_j1s_j2s' ,
        30 => 's_j2s_j1s' ,
        31 => 's_j1f_j2f' ,
        32 => 's_j2f_j1f' ,
        33 => 's_g1_a1_g1_d1' ,
        34 => 's_g1_d1_g1_a1' ,
        35 => 's_g2_b1_g2_e1' ,
        36 => 's_g2_e1_g2_b1' ,
        37 => 's_g3_c1_g3_f1' ,
        38 => 's_g3_f1_g3_c1' ,
        39 => 's_gs_g1_gs_g2' ,
        40 => 's_gs_g2_gs_g1' ,
        41 => 's_gs_g1_gs_g3' ,
        42 => 's_gs_g3_gs_g1' ,
        43 => 's_gs_g2_gs_g3' ,
        44 => 's_gs_g3_gs_g2' ,
        45 => 's_gf_g4_gf_g5' ,
        46 => 's_gf_g5_gf_g4' ,
        47 => 's_gf_g4_gf_g6' ,
        48 => 's_gf_g6_gf_g4' ,
        49 => 's_gf_g5_gf_g6' ,
        50 => 's_gf_g6_gf_g5' ,
        51 => 's_j1_a1_j1_e1' ,
        52 => 's_j1_e1_j1_a1' ,
        53 => 's_k1_j1_k1_j2' ,
        54 => 's_k1_j2_k1_j1' ,
        55 => 's_j2_c1_j2_g1' ,
        56 => 's_j2_g1_j2_c1' ,
        57 => 's_k2_j3_k2_j4' ,
        58 => 's_k2_j4_k2_j3' ,
        59 => 's_j3_b1_j3_f1' ,
        60 => 's_j3_f1_j3_b1' ,
        61 => 's_l1_j1_l1_j2' ,
        62 => 's_l1_j2_l1_j1' ,
        63 => 's_j4_d1_j4_h1' ,
        64 => 's_j4_h1_j4_d1' ,
        65 => 's_l2_j3_l2_j4' ,
        66 => 's_l2_j4_l2_j3' ,
        67 => 's_k1s_k2s' ,
        68 => 's_k2s_k1s' ,
        69 => 's_k1f_k2f' ,
        70 => 's_k2f_k1f' ,
        71 => 's_l1s_l2s' ,
        72 => 's_l2s_l1s' ,
        73 => 's_l1f_l2f' ,
        74 => 's_l2f_l1f' ,
        75 => 's_d_a1_d_b3' ,
        76 => 's_d_b3_d_a1' ,
        77 => 's_d_a1_d_c1' ,
        78 => 's_d_c1_d_a1' ,
        79 => 's_d_b3_d_c1' ,
        80 => 's_d_c1_d_b3' ,
        81 => 's_e_a2_e_b2' ,
        82 => 's_e_b2_e_a2' ,
        83 => 's_e_a2_e_c2' ,
        84 => 's_e_c2_e_a2' ,
        85 => 's_e_b2_e_c2' ,
        86 => 's_e_c2_e_b2'          
        );

    //创建竞赛界面    
    public function create_competition(){
      //加载当前用户信息
      $user=$_SESSION['think_'];
      $this->assign('user',$user);
      //设置当前的选项位置（即该选项为橙色)   
      $this->assign("biz_item_num",1);
    	$this->display();
    }

    //将创建的竞赛写入数据库
    public function create_competition_handler() {
        //导入图片上传类  
        //import('ORG.Net.UploadFile');
        $data =  I('post.');
        $com_model = D("CompetitionTable");
        //$com_model = M("Competition_table");
        if (!$com_model->create($data)){
           // 如果创建失败 表示验证没有通过 输出错误提示信息
            $this->error($com_model->getError());
        }else{
           // 验证通过 可以进行其他数据操作
           //实例化上传类
          $upload = new \Org\Net\UploadFile();  
          $upload->maxSize = 3145728;  
          //设置文件上传类型  
          $upload->allowExts = array('jpg','gif','png','jpeg');  
          //设置文件上传位置  
          //$upload->savePath = __PUBLIC__."/Img/Uploads/";
          $upload->savePath = "./Public/Img/Uploads/competitionImg/";  
          //设置文件上传名(按照时间)  
          $upload->saveRule = "time";  
          if (!$upload->upload()){  
              $this->error($upload->getErrorMsg());  
          }else{  
              //上传成功，获取上传信息  
              $info = $upload->getUploadFileInfo();  
          }
          $savename = $info[0]['savename'];
          //$imgurl = "http://www.my3w.com/yuleclub/Public/Img/Uploads/competitionImg/".$savename;
          $imgurl = "__PUBLIC__/Img/Uploads/competitionImg/".$savename;
          $data['competition_picture']=$imgurl;
          $data['competition_organizer_id']=$_SESSION['think_']['id'];
          //设置竞赛对应的竞赛模型
          $where['com_model_type']=$data['com_model_type'];
          $where['com_model_unit_number']=$data['competition_regist_unit'];
          $com_model = M('Competition_module')->field('com_model_id')->where($where)->find();
          $data['com_model_id']=$com_model['com_model_id'];
          //设置状态
          $data['competition_status']=1;
          $message = array();
          if($competition_id = M('Competition_table')->add($data)){
             $this->success('创建成功！',U('Competition/create_Competition'),5);
          }else{
            $this->error('创建失败！');
          }
          //echo json_encode($message); 
        }
        
    }

    //竞赛一览
    public function competition_list() { 
      //加载当前用户信息
      $user=$_SESSION['think_'];
      $this->assign('user',$user);
        //设置当前的选项位置（即该选项为橙色)
      $this->assign("biz_item_num",2);
      $this->com_item_num = 1;
      $where = array();
      $where['competition_organizer_id'] = $user['id']; 	
      $list = M('Competition_table')->where($where)->select();
    	//var_dump ($list);
      $this->data = $list; 
      $this->display();
    }

    //进入详细的竞赛页面--竞赛详细信息
    public function competition_detail() {
        //加载当前用户信息
        $user=$_SESSION['think_'];
        $this->assign('user',$user);
        $this->competition_id = I('get.competition_id');

        //设置当前的选项位置（即该选项为橙色)
        $this->assign("biz_item_num",2);
        $this->com_item_num = 1;
        $this->competition = M('Competition_table')->where("competition_id=%d",$this->competition_id)->find();
        
        $this->display();
    }

    //进入详细的竞赛页面--竞赛流程
    public function competition_model() {
        //加载当前用户信息
        $user=$_SESSION['think_'];
        $this->assign('user',$user);
        $this->competition_id = I('get.competition_id');
        //设置当前的选项位置（即该选项为橙色)
        $this->assign("biz_item_num",2);
        $this->com_item_num = 2;
        $this->competition = M('Competition_table')->where("competition_id=%d",$this->competition_id)->find();
        $this->competition_model = M('Competition_module')->where("com_model_id=%d",$this->competition['com_model_id'])->find();
        $this->competition_model_key = M('Competition_model_key')->order('rank asc')->select();
        //将competition_model和competition_model_key的数据归并到data中并传到前台
        $data = array();
        foreach ($this->competition_model_key as $key => $value) {
            $data[$key]['key'] = $value['key'];
            $data[$key]['remark'] = $value['remark'];
            $data[$key]['value'] = $this->competition_model[$value['key']];
        }
        $this->assign('data',$data);
        $this->display();     
    }

    //进入详细的竞赛页面--竞赛报名情况
    public function competition_regi() {
        //加载当前用户信息
        $user=$_SESSION['think_'];
        $this->assign('user',$user);
        $this->competition_id = I('get.competition_id');
        //设置当前的选项位置（即该选项为橙色)
        $this->assign("biz_item_num",2);
        $this->com_item_num = 3;
        //$competition_id = $this->competition_id;
        //$this->competition = M('Competition_table')->where($this->competition_id)->find();
        $this->user_list = M('Competition_user_info')->where("competition_id=%d",$this->competition_id)->select();
        //var_dump($this->user_list);
        $this->display();
    }

    //跳转到竞赛介绍页面
    public function competition_introduce() {
        $this->competition_id = I('get.competition_id');
        //echo $this->competition_id;
        $this->display();
    }

    //跳转到前台的竞赛列表
    public function com_home_list() {
        $this->display();
    }

    //普通用户申请参加竞赛
    public function apply_for_competition() {
        $maildata = I('post.');
        $competition = M('Competition_table')->where('competition_id=%d',$maildata['competition_id'])->find();
        $message = array();
        if($competition['competition_regist_number']>=$competition['competition_contain_number']) {
           $message['ans']=2;
        }
       //插入数据
        else if($id = M('Competition_user')->add($maildata)){        
          $competition['competition_regist_number'] = $competition['competition_regist_number']+1;
          if($competition['competition_regist_number']==$competition['competition_contain_number']) {
            $competition['competition_status'] = 2;
            $this->create_competition_clash_list($maildata['competition_id']);
          }
          if( M('Competition_table')->where(array('competition_id' => $competition['competition_id']))->save($competition)!==false){         
            $message['ans']=11;
          }else{
            $message['ans']=10;
          }    
        }else{
            $message['ans'] = 0;
        }   
        echo json_encode($message);
    }

    //自动生成第一阶段竞赛对阵列表
    public function create_competition_clash_list($competition_id) {
        $competition_id = I('get.competition_id');
        $competition = M('Competition_table')->where('competition_id=%d',$competition_id)->find();
        $com_user_list = M('Competition_user')->where('competition_id=%d',$competition_id)->order('item asc,competition_id asc')->select();
        $com_f_clash_model = M('competition_firststep_clash_model')->where('com_f_clash_unit_number=%d',$competition['competition_regist_unit'])->find();
        //按竞赛不同类型处理
        if($competition['com_model_type']==1) {
           $g_item = $com_user_list[0]['item'];
           $count=0;
           shuffle($com_user_list);
           foreach ($com_f_clash_model as $key => $value) {
               if($key!='com_f_clash_id' && $key!='com_f_clash_unit_number' && $value=='1') {
                 $row=array();
                 $row['competition_id']=$competition['competition_id'];
                 $row['position']=$this->clash_transform[$key];                
                 $row['user_id']=$com_user_list[$count]['user_id'];
                 $count++;
                 //var_dump($row);
                 if(M('competition_firststep_clash_list')->add($row)) {

                 } else {
                    //echo "clash_list add fail<br />";
                 }            
               }
           }
            $fir_record_item=array();
            $fir_record_item['competition_id'] = $competition_id;
            $fir_record_item['clash_item'] = $g_item;   
            $fir_record_item['progressing'] = 0;
            var_dump($fir_record_item);
            if(M('Com_single_secondstep_record')->add($fir_record_item)){

            }else {
               //echo "Com_secondstep_record add fail<br />";
            }

        } else if ($competition['com_model_type']==2) {
            $unit = $competition['competition_regist_unit'];
            $com_user_list_f = array();
            $com_user_list_s = array();
            $com_user_list_t = array();

            $item1= $com_user_list[0]['item'];
            $item2= $com_user_list[0+$unit]['item'];
            $item3= $com_user_list[0+ 2*$unit]['item'];

            for($i=0;$i<$unit;$i++) {
            $com_user_list_f[$i] =  $com_user_list[$i];
            $com_user_list_s[$i] =  $com_user_list[$i+$unit];
            $com_user_list_t[$i] =  $com_user_list[$i+ 2*$unit];
           }
           $count=0;
           shuffle($com_user_list_f);
           shuffle($com_user_list_s);
           shuffle($com_user_list_t);
           $rowlist=array();
           foreach ($com_f_clash_model as $key => $value) {
               if($key!='com_f_clash_id' && $key!='com_f_clash_unit_number' && $value=='1') {
                 $row1=array();
                 $row2=array();
                 $row3=array();
                 $row1['competition_id']=$row2['competition_id']=$row3['competition_id']=$competition['competition_id'];
                 $row1['position']=$row2['position']=$row3['position']=$this->clash_transform[$key];                
                 $row1['user_id']=$com_user_list_f[$count]['user_id'];
                 $row2['user_id']=$com_user_list_s[$count]['user_id'];
                 $row3['user_id']=$com_user_list_t[$count]['user_id'];
                 $count++;
                 array_push($rowlist,$row1,$row2,$row3);
               }
           }
           if(M('Competition_firststep_clash_list')->addAll($rowlist)) {
                //echo "clash_list add success<br />";
                //初始化三项赛记录情况
                $sec_record_item=array();
                $sec_record_item['competition_id'] = $competition_id;
                $sec_record_item['clash_item1'] = $item1;
                $sec_record_item['clash_item2'] = $item2;
                $sec_record_item['clash_item3'] = $item3;
                $sec_record_item['progressing1'] = 0;
                $sec_record_item['progressing2'] = 0;
                $sec_record_item['progressing3'] = 0;
                if(M('Com_secondstep_record')->add($sec_record_item)){

                }else {
                   //echo "Com_secondstep_record add fail<br />";
                }
              
            } else {
               //echo "clash_list add fail<br />";
            }  
       }
   }

   //管理端成绩表查看
   public function create_competition_clash_result() {
        //加载当前用户信息
        $user=$_SESSION['think_'];
        $this->assign('user',$user);

        //设置是否为修改转态
        $this->assign('mod_item',1);
        $this->assign('mod_s_item',1);
       
        $this->competition_id = I("get.competition_id");
        $this->select_item = I("get.select_item");
        
        //设置当前的选项位置（即该选项为橙色)
        $this->assign("biz_item_num",2);
        $this->com_item_num = 4;

        $this->competition = M('Competition_table')->where('competition_id=%d',$this->competition_id)->find();
        $this->assign('unit',$this->competition['competition_regist_unit']);
        $com_users=array();
        $com_f_clash_list=array();
        $tr_com_f_clash_list=array();
        $participant_list=array();
        $score_f_list=array();
        $score_s_list=array();

          $com_f_clash_list=M('competition_firststep_clash_list')->where('competition_id=%d',$this->competition_id)->select(); 
          foreach ($com_f_clash_list as $key => $value) {
            $tr_com_f_clash_list[$value['user_id']]=  $this->t_clash_transform[$value['position']];
          }
          $com_users=M('competition_user_info')->field('competition_id,user_id,item,name')->where('competition_id=%d',$this->competition_id)->order('item asc')->select();
          $i= 0+$this->select_item*$this->competition['competition_regist_unit'];
          $c=($this->select_item+1)*$this->competition['competition_regist_unit'];
          
          //读取竞赛成绩
          $score_where=array();
          $score_where['competition_id']=$this->competition_id;
          $score_where['g_item']=$com_users[$i]['item'];
          $this->assign('cur_item',$com_users[$i]['item']);
          $com_f_score_list=M('Competition_firststep_clash_score')->where($score_where)->select();
          
          //输出当前比赛进行阶段
        if($this->competition['com_model_type']==2) {
           $scr_where1 = array();
           $scr_where1['competition_id'] = $this->competition_id;
           $com_secondstep_record = M('Com_secondstep_record')->where($scr_where1)->find();
           if($com_secondstep_record['clash_item1']==$com_users[$i]['item']) {
             $this->assign('com_record',$com_secondstep_record['progressing1']);
           }else if($com_secondstep_record['clash_item2']==$com_users[$i]['item']){
             $this->assign('com_record',$com_secondstep_record['progressing2']);
           }else if($com_secondstep_record['clash_item3']==$com_users[$i]['item']){
             $this->assign('com_record',$com_secondstep_record['progressing3']);
           }
        } else if($competition['com_model_type']==1) {
           $s_scr_where = array();
           $s_scr_where['competition_id'] = $this->competition_id;
           $com_sin_secondstep_record = M('Com_single_secondstep_record')->where($s_scr_where)->find();
           if($com_sin_secondstep_record['progressing'] == 1) {
              $this->assign('com_record',$com_sin_secondstep_record['progressing']);
           }
       }


          //读取第二阶段成绩
          $s_score_where=array();
          $s_score_where['competition_id']=$this->competition_id;
          $s_score_where['g_item']=$com_users[$i]['item'];
          $com_s_score_list=M('Competition_secondstep_clash_score')->where($s_score_where)->select();
          
          //var_dump($com_s_score_list);

          //展示第二阶段对阵名单
          $group_num = $this->group_num_transform[$this->competition['competition_regist_unit']];
          $csc_where = array();
          $csc_where['competition_id'] = $this->competition_id;
          $csc_where['g_item'] = $com_users[$i]['item'];
          $com_s_clash_group_list = M('Competition_secondstep_clash_'.$group_num.'group_list')->where($csc_where)->find();
          //var_dump($com_s_clash_group_list);

          //展示第一阶段名次
          $cff_where=array();
          $cff_where['competition_id'] = $this->competition_id;
          $cff_where['g_item'] = $com_users[$i]['item'];
          $com_f_final_score_list = M('Competition_firststep_final_score')->where($cff_where)->select();
          //var_dump($com_f_final_score_list);
          $tr_com_f_final_score_list = array();

          //重构第一第二阶段成绩数组
          foreach ($com_f_score_list as $key => $value) {
              $score_f_list[$this->tr_score_mode_transform[$value['mode']]] = $value['score'];
          }
          $this->assign('score_f_list',$score_f_list);
          foreach ($com_s_score_list as $key => $value) {
              $score_s_list[$this->tr_score_second_mode_transform[$value['mode']]] = $value['score'];
          }
          $this->assign('score_s_list',$score_s_list);

          //var_dump($score_s_list);

          foreach ($com_f_final_score_list as $key => $value) {
              $tr_com_f_final_score_list[$this->t_clash_transform[$value['position']]]['wins'] = $value['wins'];
              $tr_com_f_final_score_list[$this->t_clash_transform[$value['position']]]['net_wins'] = $value['net_wins'];
              $tr_com_f_final_score_list[$this->t_clash_transform[$value['position']]]['rank'] = $value['rank'];
          }

        
          //展示第二阶段名次
          $com_s_g3_final_score_list=array();
          $com_s_g6_final_score_list=array();
          $tr_com_s_g3_final_score_list = array();
          $tr_com_s_g6_final_score_list = array();
          if($group_num==3) {
             $csf_where=array();
             $csf_where['competition_id'] = $this->competition_id;
             $csf_where['g_item'] = $com_users[$i]['item'];
             $com_s_g3_final_score_list = M('Competition_secondstep_group3_final_score')->where($csf_where)->select();
             foreach ($com_s_g3_final_score_list as $key => $value) {
                $tr_com_s_g3_final_score_list[$this->tr_clash_second_transform[$value['position']]]['wins'] = $value['wins'];
                $tr_com_s_g3_final_score_list[$this->tr_clash_second_transform[$value['position']]]['net_wins'] = $value['net_wins'];
                $tr_com_s_g3_final_score_list[$this->tr_clash_second_transform[$value['position']]]['rank'] = $value['rank'];
            }
          }

          if($group_num==6) {
              $csf_where=array();
              $csf_where['competition_id'] = $this->competition_id;
              $csf_where['g_item'] = $com_users[$i]['item'];
              $com_s_g6_final_score_list = M('Competition_secondstep_group6_final_score')->where($csf_where)->select();
              foreach ($com_s_g6_final_score_list as $key => $value) {
                $tr_com_s_g6_final_score_list[$this->tr_clash_second_transform[$value['position']]]['wins'] = $value['wins'];
                $tr_com_s_g6_final_score_list[$this->tr_clash_second_transform[$value['position']]]['net_wins'] = $value['net_wins'];
                $tr_com_s_g6_final_score_list[$this->tr_clash_second_transform[$value['position']]]['rank'] = $value['rank'];
            }
          }

          //重构用户信息数组
          for(;$i<$c;$i++) {
             $pos=$tr_com_f_clash_list[$com_users[$i]['user_id']];
             $participant_list[$pos]['name']=$com_users[$i]['name'];
             $participant_list[$pos]['wins']=$tr_com_f_final_score_list[$pos]['wins'];
             $participant_list[$pos]['net_wins']=$tr_com_f_final_score_list[$pos]['net_wins'];
             $participant_list[$pos]['rank']=$tr_com_f_final_score_list[$pos]['rank'];
          }

          
         //构造第二阶段用户信息
         foreach ($com_s_clash_group_list as $key => $value) {
             if($key!="competition_id"&&$key!='com_unit'&&$key!='g_item') {
                $where['id'] = $value;
                $name_arr = M('User_table')->where($where)->field('name')->find();
                //var_dump($name_arr);
                $participant_list[$key]['name'] = $name_arr['name'];
                if($group_num==3) {
                   $participant_list[$key]['wins']=$tr_com_s_g3_final_score_list[$key]['wins'];
                   $participant_list[$key]['net_wins']=$tr_com_s_g3_final_score_list[$key]['net_wins'];
                   $participant_list[$key]['rank']=$tr_com_s_g3_final_score_list[$key]['rank'];
                }
                if($group_num==6) {
                   $participant_list[$key]['wins']=$tr_com_s_g6_final_score_list[$key]['wins'];
                   $participant_list[$key]['net_wins']=$tr_com_s_g6_final_score_list[$key]['net_wins'];
                   $participant_list[$key]['rank']=$tr_com_s_g6_final_score_list[$key]['rank'];
                }
             }
         }

            $this->assign('ulist',$participant_list);
            $this->assign('group_num',$group_num);
        $this->display();
   }

    //成绩表填写
    public function modify_competition_clash_result() {
        //加载当前用户信息
        $user=$_SESSION['think_'];
        $this->assign('user',$user);
        
        $this->competition_id = I("get.competition_id");
        $this->select_item = I("get.select_item");

        //设置当前的选项位置（即该选项为橙色)
        $this->assign("biz_item_num",2);
        $this->com_item_num = 4;
        
        $this->competition = M('Competition_table')->where('competition_id=%d',$this->competition_id)->find();
        $this->assign('unit',$this->competition['competition_regist_unit']);
        $com_users=array();
        $com_f_clash_list=array();
        $tr_com_f_clash_list=array();
        $participant_list=array();
        $score_f_list=array();
        $score_s_list=array();

        if($this->competition['competition_status']>=2) {

            $com_f_clash_list=M('competition_firststep_clash_list')->where('competition_id=%d',$this->competition_id)->select(); 
            foreach ($com_f_clash_list as $key => $value) {
                $tr_com_f_clash_list[$value['user_id']]=  $this->t_clash_transform[$value['position']];
            }
            $com_users=M('competition_user_info')->field('competition_id,user_id,item,name')->where('competition_id=%d',$this->competition_id)->order('item asc')->select();
            $i= 0+$this->select_item*$this->competition['competition_regist_unit'];
            $c=($this->select_item+1)*$this->competition['competition_regist_unit'];
          
            //读取竞赛成绩
            $score_where=array();
            $score_where['competition_id']=$this->competition_id;
            $score_where['g_item']=$com_users[$i]['item'];
            $this->assign('cur_item',$com_users[$i]['item']);
            $group_item = $com_users[$i]['item'];
          
            $com_f_score_list=M('Competition_firststep_clash_score')->where($score_where)->select();
          

          //读取第二阶段成绩
          $s_score_where=array();
          $s_score_where['competition_id']=$this->competition_id;
          $s_score_where['g_item']=$com_users[$i]['item'];
          $com_s_score_list=M('Competition_secondstep_clash_score')->where($s_score_where)->select();

          //展示第二阶段对阵名单
          $group_num = $this->group_num_transform[$this->competition['competition_regist_unit']];
          $csc_where = array();
          $csc_where['competition_id'] = $this->competition_id;
          $csc_where['g_item'] = $com_users[$i]['item'];
          $com_s_clash_group_list = M('Competition_secondstep_clash_'.$group_num.'group_list')->where($csc_where)->find();
          //var_dump($com_s_clash_group_list);

          //展示第一阶段名次
          $cff_where=array();
          $cff_where['competition_id'] = $this->competition_id;
          $cff_where['g_item'] = $com_users[$i]['item'];
          $com_f_final_score_list = M('Competition_firststep_final_score')->where($cff_where)->select();
          //var_dump($com_f_final_score_list);
          $tr_com_f_final_score_list = array();

          //重构第一第二阶段成绩数组
          foreach ($com_f_score_list as $key => $value) {
              $score_f_list[$this->tr_score_mode_transform[$value['mode']]] = $value['score'];
          }
          $this->assign('score_f_list',$score_f_list);
          foreach ($com_s_score_list as $key => $value) {
              $score_s_list[$this->tr_score_second_mode_transform[$value['mode']]] = $value['score'];
          }
          $this->assign('score_s_list',$score_s_list);

            foreach ($com_f_final_score_list as $key => $value) {
                $tr_com_f_final_score_list[$this->t_clash_transform[$value['position']]]['wins'] = $value['wins'];
                $tr_com_f_final_score_list[$this->t_clash_transform[$value['position']]]['net_wins'] = $value['net_wins'];
                $tr_com_f_final_score_list[$this->t_clash_transform[$value['position']]]['rank'] = $value['rank'];
            }

         //展示第二阶段名次
          $com_s_g3_final_score_list=array();
          $com_s_g6_final_score_list=array();
          $tr_com_s_g3_final_score_list = array();
          $tr_com_s_g6_final_score_list = array();
          if($group_num==3) {
             $csf_where=array();
             $csf_where['competition_id'] = $this->competition_id;
             $csf_where['g_item'] = $com_users[$i]['item'];
             $com_s_g3_final_score_list = M('Competition_secondstep_group3_final_score')->where($csf_where)->select();
             foreach ($com_s_g3_final_score_list as $key => $value) {
                $tr_com_s_g3_final_score_list[$this->tr_clash_second_transform[$value['position']]]['wins'] = $value['wins'];
                $tr_com_s_g3_final_score_list[$this->tr_clash_second_transform[$value['position']]]['net_wins'] = $value['net_wins'];
                $tr_com_s_g3_final_score_list[$this->tr_clash_second_transform[$value['position']]]['rank'] = $value['rank'];
            }
          }

          if($group_num==6) {
              $csf_where=array();
              $csf_where['competition_id'] = $this->competition_id;
              $csf_where['g_item'] = $com_users[$i]['item'];
              $com_s_g6_final_score_list = M('Competition_secondstep_group6_final_score')->where($csf_where)->select();
              foreach ($com_s_g6_final_score_list as $key => $value) {
                $tr_com_s_g6_final_score_list[$this->tr_clash_second_transform[$value['position']]]['wins'] = $value['wins'];
                $tr_com_s_g6_final_score_list[$this->tr_clash_second_transform[$value['position']]]['net_wins'] = $value['net_wins'];
                $tr_com_s_g6_final_score_list[$this->tr_clash_second_transform[$value['position']]]['rank'] = $value['rank'];
            }
          }

            //重构用户信息数组
            for(;$i<$c;$i++) {
                $pos=$tr_com_f_clash_list[$com_users[$i]['user_id']];
                $participant_list[$pos]['name']=$com_users[$i]['name'];
                $participant_list[$pos]['wins']=$tr_com_f_final_score_list[$pos]['wins'];
                $participant_list[$pos]['net_wins']=$tr_com_f_final_score_list[$pos]['net_wins'];
                $participant_list[$pos]['rank']=$tr_com_f_final_score_list[$pos]['rank'];
            }


         //构造第二阶段用户信息
         foreach ($com_s_clash_group_list as $key => $value) {
             if($key!="competition_id"&&$key!='com_unit'&&$key!='g_item') {
                $where['id'] = $value;
                $name_arr = M('User_table')->where($where)->field('name')->find();
                //var_dump($name_arr);
                $participant_list[$key]['name'] = $name_arr['name'];
                if($group_num==3) {
                   $participant_list[$key]['wins']=$tr_com_s_g3_final_score_list[$key]['wins'];
                   $participant_list[$key]['net_wins']=$tr_com_s_g3_final_score_list[$key]['net_wins'];
                   $participant_list[$key]['rank']=$tr_com_s_g3_final_score_list[$key]['rank'];
                }
                if($group_num==6) {
                   $participant_list[$key]['wins']=$tr_com_s_g6_final_score_list[$key]['wins'];
                   $participant_list[$key]['net_wins']=$tr_com_s_g6_final_score_list[$key]['net_wins'];
                   $participant_list[$key]['rank']=$tr_com_s_g6_final_score_list[$key]['rank'];
                }
             }
         }
          $this->assign('ulist',$participant_list);
     }     

        
        //按比赛类型处理
        if($this->competition['com_model_type'] == 2) {
            $s_r_where=array();
            $s_r_where['competition_id'] = $this->competition_id;
            $sec_record = M('Com_secondstep_record')->where($s_r_where)->find();
            //设置第一、二阶段是否为修改转态
            if($sec_record['clash_item1'] == $group_item) {
                if($sec_record['progressing1']>=1) {
                    $this->assign('mod_item',1);  
                    $this->assign('mod_s_item',2);
                } else {
                    $this->assign('mod_item',2);  
                    $this->assign('mod_s_item',1);
                }
            } else if($sec_record['clash_item2'] == $group_item) {
                if($sec_record['progressing2']>=1) {
                    $this->assign('mod_item',1);  
                    $this->assign('mod_s_item',2);
                } else {
                    $this->assign('mod_item',2);  
                    $this->assign('mod_s_item',1);
                }
            } else if($sec_record['clash_item3'] == $group_item) {
                if($sec_record['progressing3']>=1) {
                    $this->assign('mod_item',1);  
                    $this->assign('mod_s_item',2);
                } else {
                    $this->assign('mod_item',2);  
                    $this->assign('mod_s_item',1);
                }
            } else {
                $this->assign('mod_item',2);  
                $this->assign('mod_s_item',2);
            }
        } else if($this->competition['com_model_type'] == 1) {
          $s_s_r_where=array();
          $s_s_r_where['competition_id'] = $this->competition_id;
          $s_sec_record = M('Com_single_secondstep_record')->where($s_s_r_where)->find();
          if($s_sec_record['progressing']>=1) {
            $this->assign('mod_item',1);  
             $this->assign('mod_s_item',2); 
          } else {
             $this->assign('mod_item',2);  
            $this->assign('mod_s_item',1);
         }
       } 
        $this->display();
    
  }


   //统计分数并进入第二阶段
   public function score_count_to_second() {


     $this->competition_id = I('get.competition_id');
     $this->select_item = I('get.select_item');
     $this->cur_item = I('get.cur_item');

     $com_where['competition_id'] = $this->competition_id;
     $competition = M('Competition_table')->where($com_where)->find();

     $com_secondstep_record=array();
     $com_sin_secondstep_record=array();

     if($competition['com_model_type']==2) {
     $scr_where1 = array();
     $scr_where1['competition_id'] = $this->competition_id;
     $com_secondstep_record = M('Com_secondstep_record')->where($scr_where1)->find();
     if($com_secondstep_record['clash_item1']==$this->cur_item) {
        if($com_secondstep_record['progressing1'] >= 1) {
            $this->success('竞赛已进入第二阶段，不可重复操作！',U('Competition/create_competition_clash_result',array('competition_id'=>$this->competition_id,'select_item'=>0)),5);
            return ;
        }
     }else if($com_secondstep_record['clash_item2']==$this->cur_item){
        if($com_secondstep_record['progressing2'] >= 1) {
            $this->success('竞赛已进入第二阶段，不可重复操作！',U('Competition/create_competition_clash_result',array('competition_id'=>$this->competition_id,'select_item'=>0)),5);
            return ;
        }
     }else if($com_secondstep_record['clash_item3']==$this->cur_item){
        if($com_secondstep_record['progressing3'] >= 1) {
            $this->success('竞赛已进入第二阶段，不可重复操作！',U('Competition/create_competition_clash_result',array('competition_id'=>$this->competition_id,'select_item'=>0)),5);
            return ;
        }
     }
 } else if($competition['com_model_type']==1) {
     $s_scr_where = array();
     $s_scr_where['competition_id'] = $this->competition_id;
     $com_sin_secondstep_record = M('Com_single_secondstep_record')->where($s_scr_where)->find();
     if($com_sin_secondstep_record['progressing'] == 1) {
        $this->success('竞赛已进入第二阶段，不可重复操作！',U('Competition/create_competition_clash_result',array('competition_id'=>$this->competition_id,'select_item'=>0)),5);
        return ;
     }
 }

     $sc_where = array();
     $sc_where['competition_id'] = $this->competition_id;
     $sc_where['g_item'] = $this->cur_item;
     $first_score = M('Competition_firststep_clash_score')->where($sc_where)->select();
     
     //var_dump($first_score);
     
     $tran_first_score = array();
     foreach ($first_score as $key => $value) {
        $tran_first_score[$this->tr_score_mode_transform[$value['mode']]] = $value['score'];
     }
     $v_where = array();
     $v_where['competition_id'] = $this->competition_id;
     $v_where['item'] = $this->cur_item;
     $com_user_pos_list = M('Com_user_position_view')->where($v_where)->select();
     
     //var_dump($com_user_pos_list);
     
     $tmp_wins_list=array();
     $tmp_losts_list=array();
     $tmp_net_wins_list=array();
     $pos_user_id = array();
     $rank_list=array();
     $score_rank_list = array();

     foreach ($com_user_pos_list as $key => $value) {
         $pos_user_id[$value['position']] = $value['user_id'];
         $tmp_wins_list[substr($this->t_clash_transform[$value['position']], 2)]=0;
         $tmp_losts_list[substr($this->t_clash_transform[$value['position']], 2)]=0;
         $rank_list[substr($this->t_clash_transform[$value['position']], 2)]=0;
         $score_rank_list[substr($this->t_clash_transform[$value['position']], 2)]=0;
     }
     foreach ($tran_first_score as $key => $value) {
         $pre_pos = substr($key, 2, 2);
         $post_pos = substr($key, 5, 2);
         $tmp_wins_list[$pre_pos] += $value;
         $tmp_losts_list[$post_pos] += $value;
     }

     $tran_wins_list = array();
     $tran_wins_list['a'] = array();
     $tran_wins_list['b'] = array();
     $tran_wins_list['c'] = array();
     $tran_wins_list['d'] = array();
     $tran_wins_list['e'] = array();
     $tran_wins_list['f'] = array();
     $tran_wins_list['g'] = array();
     $tran_wins_list['h'] = array();
     foreach ($tmp_wins_list as $key => $value) {
         $tmp_net_wins_list[$key] = $value - $tmp_losts_list[$key];
         $score_rank_list[$key] = $value*100+$tmp_net_wins_list[$key]+20;
         $tran_wins_list[substr($key,0,1)][$key] = $score_rank_list[$key];
     }

     //var_dump($tran_wins_list);

     $tmp_tran_wins_list = $tran_wins_list;
     foreach ($tmp_tran_wins_list as $key => $value) {
         $tmp_tmp_wins_list = $value;
         $count =1;
         while (sizeof($tmp_tmp_wins_list)!=0) {
            $r_key = array_search(max($tmp_tmp_wins_list), $tmp_tmp_wins_list);
            $rank_list[$r_key] = $count;
            $count ++;
            unset($tmp_tmp_wins_list[$r_key]); 
        }
     }
     
     foreach ($tmp_wins_list as $key => $value) {
         $row = array();
         $row['competition_id'] = $this->competition_id;
         $row['position'] = $this->clash_transform["f_".$key];
         $row['user_id'] = $pos_user_id[$row['position']];
         $row['wins'] = $value;
         $row['net_wins'] = $tmp_net_wins_list[$key];
         $row['rank'] = $rank_list[$key];
         $row['g_item'] = $this->cur_item;
         //var_dump($row);
         
         if(!M('Competition_firststep_final_score')->add($row)) {

         }
         
     }

     //$scr_where2 = array();
     //$scr_where2['competition_id'] = $this->competition_id;
     //$com_secondstep_record = M('Com_secondstep_record')->where($scr_where2)->find();
if($competition['com_model_type']==2){
     if($com_secondstep_record['clash_item1']==$this->cur_item) {
        $com_secondstep_record['progressing1'] = 1;
     }else if($com_secondstep_record['clash_item2']==$this->cur_item){
        $com_secondstep_record['progressing2'] = 1;
     }else if($com_secondstep_record['clash_item3']==$this->cur_item){
        $com_secondstep_record['progressing3'] = 1;
     }

     $scr_where = array();
     $scr_where['competition_id'] = $this->competition_id;
     if(!M('Com_secondstep_record')->where($scr_where)->save($com_secondstep_record)) {

     }

 } else if($competition['com_model_type']==1) {
     $s_scr_where = array();
     $s_scr_where['competition_id'] = $this->competition_id;
     $com_sin_secondstep_record['progressing'] = 1;
     if(!M('Com_single_secondstep_record')->where($s_scr_where)->save($com_sin_secondstep_record)) {

     }
 }
     
     

     
     $group_num = $this->group_num_transform[$competition['competition_regist_unit']];
     
     //var_dump($rank_list);
     
     $second_out= array();
     $second_out['a'] = array();
     $second_out['b'] = array();
     $second_out['c'] = array();
     $second_out['d'] = array();
     $second_out['e'] = array();
     $second_out['f'] = array();
     $second_out['g'] = array();
     $second_out['h'] = array();
     foreach ($rank_list as $key => $value) {
        if($value == 1||$value == 2)
         $second_out[substr($key,0,1)][$value] = $key;
     }
     
     switch ($group_num) {
         case 2:
             $row = array();
             $row['competition_id'] = $this->competition_id;
             $row['com_unit'] = $competition['competition_regist_unit'];
             $row['g_item'] = $this->cur_item; 
             $row['c_a1'] = $pos_user_id[$this->clash_transform["f_".$second_out['a'][1]]];
             $row['c_b2'] = $pos_user_id[$this->clash_transform["f_".$second_out['b'][2]]];
             $row['d_a2'] = $pos_user_id[$this->clash_transform["f_".$second_out['a'][2]]];
             $row['d_b1'] = $pos_user_id[$this->clash_transform["f_".$second_out['b'][1]]];
             M('competition_secondstep_clash_'.$group_num.'group_list')->add($row);
             break;

         case 3:
             $row = array();
             $row['competition_id'] = $this->competition_id;
             $row['com_unit'] = $competition['competition_regist_unit'];
             $row['g_item'] = $this->cur_item;
             $row['d_a1'] = $pos_user_id[$this->clash_transform["f_".$second_out['a'][1]]];
             $row['d_b3'] = $pos_user_id[$this->clash_transform["f_".$second_out['b'][1]]];
             $row['d_c1'] = $pos_user_id[$this->clash_transform["f_".$second_out['c'][1]]];
             $row['e_a2'] = $pos_user_id[$this->clash_transform["f_".$second_out['a'][2]]];
             $row['e_b2'] = $pos_user_id[$this->clash_transform["f_".$second_out['b'][2]]];
             $row['e_c2'] = $pos_user_id[$this->clash_transform["f_".$second_out['c'][2]]];
             M('competition_secondstep_clash_'.$group_num.'group_list')->add($row);
             break;

         case 4:
             $row = array();
             $row['competition_id'] = $this->competition_id;
             $row['com_unit'] = $competition['competition_regist_unit'];
             $row['g_item'] = $this->cur_item;
             $row['e1_a1'] = $pos_user_id[$this->clash_transform["f_".$second_out['a'][1]]];
             $row['e1_c2'] = $pos_user_id[$this->clash_transform["f_".$second_out['c'][2]]];
             $row['e2_c1'] = $pos_user_id[$this->clash_transform["f_".$second_out['c'][1]]];
             $row['e2_a2'] = $pos_user_id[$this->clash_transform["f_".$second_out['a'][2]]];
             $row['f1_b1'] = $pos_user_id[$this->clash_transform["f_".$second_out['b'][1]]];
             $row['f1_d2'] = $pos_user_id[$this->clash_transform["f_".$second_out['d'][2]]];
             $row['f2_d1'] = $pos_user_id[$this->clash_transform["f_".$second_out['d'][1]]];
             $row['f2_b2'] = $pos_user_id[$this->clash_transform["f_".$second_out['b'][2]]];
             M('competition_secondstep_clash_'.$group_num.'group_list')->add($row);
             break;

         case 6:
             $row = array();
             $row['competition_id'] = $this->competition_id;
             $row['com_unit'] = $competition['competition_regist_unit'];
             $row['g_item'] = $this->cur_item;
             $row['g_a1'] = $pos_user_id[$this->clash_transform["f_".$second_out['a'][1]]];
             $row['g_d1'] = $pos_user_id[$this->clash_transform["f_".$second_out['d'][1]]];
             $row['g_b1'] = $pos_user_id[$this->clash_transform["f_".$second_out['b'][1]]];
             $row['g_e1'] = $pos_user_id[$this->clash_transform["f_".$second_out['e'][1]]];
             $row['g_c1'] = $pos_user_id[$this->clash_transform["f_".$second_out['c'][1]]];
             $row['g_f1'] = $pos_user_id[$this->clash_transform["f_".$second_out['f'][1]]];             
             M('competition_secondstep_clash_'.$group_num.'group_list')->add($row);
             break;

         case 8:
             $row = array();
             $row['competition_id'] = $this->competition_id;
             $row['com_unit'] = $competition['competition_regist_unit'];
             $row['g_item'] = $this->cur_item;
             $row['j1_a1'] = $pos_user_id[$this->clash_transform["f_".$second_out['a'][1]]];
             $row['j1_e1'] = $pos_user_id[$this->clash_transform["f_".$second_out['e'][1]]];
             $row['j2_c1'] = $pos_user_id[$this->clash_transform["f_".$second_out['c'][1]]];
             $row['j2_g1'] = $pos_user_id[$this->clash_transform["f_".$second_out['g'][1]]];
             $row['j3_b1'] = $pos_user_id[$this->clash_transform["f_".$second_out['b'][1]]];
             $row['j3_f1'] = $pos_user_id[$this->clash_transform["f_".$second_out['f'][1]]];
             $row['j4_d1'] = $pos_user_id[$this->clash_transform["f_".$second_out['d'][1]]];
             $row['j4_h1'] = $pos_user_id[$this->clash_transform["f_".$second_out['h'][1]]];
             M('competition_secondstep_clash_'.$group_num.'group_list')->add($row);
             break;
         
         default:
             # code...
             break;
     }

     $this->success('保存成功！',U('Competition/create_competition_clash_result',array('competition_id'=>$this->competition_id,'select_item'=>0)),5);     

   }

   //统计分数并进入第二阶段(第二轮)
   public function score_count_to_second_two() {
     
     $this->competition_id = I('get.competition_id');
     $this->select_item = I('get.select_item');
     $this->cur_item = I('get.cur_item');

     $com_where['competition_id'] = $this->competition_id;
     $competition = M('Competition_table')->where($com_where)->find();
     
     //用于读取竞赛记录状况
     $com_secondstep_record=array();
     $com_sin_secondstep_record=array();
     
     //判断比赛类型
     if($competition['com_model_type']==2) {

       //读取比赛情况记录 
       $scr_where1 = array();
       $scr_where1['competition_id'] = $this->competition_id;
       $com_secondstep_record = M('Com_secondstep_record')->where($scr_where1)->find();
       if($com_secondstep_record['clash_item1']==$this->cur_item) {
            if($com_secondstep_record['progressing1'] >= 2) {
                $this->success('竞赛已进入第二阶段（第二轮），不可重复操作！',U('Competition/create_competition_clash_result',array('competition_id'=>$this->competition_id,'select_item'=>0)),5);
                return ;
            } 
         }else if($com_secondstep_record['clash_item2']==$this->cur_item){
           if($com_secondstep_record['progressing2'] >= 2) {
                $this->success('竞赛已进入第二阶段（第二轮），不可重复操作！',U('Competition/create_competition_clash_result',array('competition_id'=>$this->competition_id,'select_item'=>0)),5);
                return ;
            }
         }else if($com_secondstep_record['clash_item3']==$this->cur_item){
             if($com_secondstep_record['progressing3'] >= 2) {
                $this->success('竞赛已进入第二阶段（第二轮），不可重复操作！',U('Competition/create_competition_clash_result',array('competition_id'=>$this->competition_id,'select_item'=>0)),5);
                return ;
            }
         }
    } else if($competition['com_model_type']==1) {
       //读取比赛情况记录 
       $s_scr_where = array();
       $s_scr_where['competition_id'] = $this->competition_id;
       $com_sin_secondstep_record = M('Com_single_secondstep_record')->where($s_scr_where1)->find();
       
       if($com_sin_secondstep_record['progressing'] >=2 ) {
          $this->success('竞赛已进入第二阶段（第二轮），不可重复操作！',U('Competition/create_competition_clash_result',array('competition_id'=>$this->competition_id,'select_item'=>0)),5);
          return ;
       }
     }

     $sc_where = array();
     $sc_where['Competition_id'] = $this->competition_id;
     $sc_where['g_item'] = $this->cur_item;
     $second_score = M('Competition_secondstep_clash_score')->where($sc_where)->select();
     
     //var_dump($second_score);
     $tran_second_score = array();
     foreach ($second_score as $key => $value) {
        if(array_search($value['mode'],$this->second_first_clash)!=false&&array_search($value['mode'],$this->second_first_clash)!=null) {
            $tran_second_score[$this->tr_score_second_mode_transform[$value['mode']]] = $value['score'];
        }
        
     }

     //var_dump($tran_second_score);

     $group_num = $this->group_num_transform[$competition['competition_regist_unit']];
     
     $scl_where=array();
     $scl_where['competition_id']= $this->competition_id;
     $scl_where['g_item'] = $this->cur_item;
     $sec_clash_list = M('Competition_secondstep_clash_'.$group_num.'group_list')->where($scl_where)->select();

     //var_dump($sec_clash_list);
     $com_user_pos_list = array();

     //构造参赛人员与对应位置数组
     foreach ($sec_clash_list[0] as $key => $value) {
              if($key!='competition_id'&&$key!='com_unit'&&$key!='g_item'&&$value!=null) {
                if(array_search($key,$this->second_first_position)!=false&&array_search($key, $this->second_first_position)!=null) {
                  $row = array();
                  $row['user_id'] = $value;
                  $row['competition_id'] = $this->competition_id;
                  $row['item'] = $this->cur_item;
                  $row['position'] = $this->clash_second_transform[$key];
                  //var_dump($row);
                  array_push($com_user_pos_list,$row);
                }
              }
          }


     
     $tmp_wins_list=array();
     $tmp_losts_list=array();
     $tmp_net_wins_list=array();
     $pos_user_id = array();
     $rank_list=array();
     $score_rank_list = array();
    
    foreach ($com_user_pos_list as $key => $value) {
         $pos_user_id[$value['position']] = $value['user_id'];
         $tmp_wins_list[$this->tr_clash_second_transform[$value['position']]]=0;
         $tmp_losts_list[$this->tr_clash_second_transform[$value['position']]]=0;
         $rank_list[$this->tr_clash_second_transform[$value['position']]]=0;
         $score_rank_list[$this->tr_clash_second_transform[$value['position']]]=0;
     }

     //var_dump($pos_user_id);
     //echo "11111111111111111111111111111";

    foreach ($tran_second_score as $key => $value) {
         $strs = explode('_', $key);
         //var_dump($strs);
         $pre_pos = $strs[1]."_".$strs[2];
         $post_pos = $strs[3]."_".$strs[4];
         $tmp_wins_list[$pre_pos] += $value;
         $tmp_losts_list[$post_pos] += $value;
     }

     //var_dump($tmp_wins_list);
     //var_dump($tmp_losts_list);
     $tran_wins_list = array();
     switch ($group_num) {
         case '2':
             $tran_wins_list['c'] = array();
             $tran_wins_list['d'] = array();
             break;
         
         case '3':
             $tran_wins_list['d'] = array();
             $tran_wins_list['e'] = array();
             break;

         case '4':
             $tran_wins_list['e1'] = array();
             $tran_wins_list['e2'] = array();
             $tran_wins_list['f1'] = array();
             $tran_wins_list['f2'] = array();             
             break;

         case '6':
            $tran_wins_list['g1'] = array();
            $tran_wins_list['g2'] = array();
            $tran_wins_list['g3'] = array();
             break;

         case '8':
             $tran_wins_list['j1'] = array();
             $tran_wins_list['j2'] = array();
             $tran_wins_list['j3'] = array();
             $tran_wins_list['j4'] = array();
             break;

         default:
             # code...
             break;
     }

     foreach ($tmp_wins_list as $key => $value) {
         $tmp_net_wins_list[$key] = $value - $tmp_losts_list[$key];
         $score_rank_list[$key] = $value*100+$tmp_net_wins_list[$key]+20;
         if($group_num==2||$group_num==3) {
            $tran_wins_list[substr($key,0,1)][$key] = $score_rank_list[$key];
         } else {
            $tran_wins_list[substr($key,0,2)][$key] = $score_rank_list[$key];
         }
     }

     $tmp_tran_wins_list = $tran_wins_list;
     foreach ($tmp_tran_wins_list as $key => $value) {
         $tmp_tmp_wins_list = $value;
         $count =1;
         while (sizeof($tmp_tmp_wins_list)!=0) {
            $r_key = array_search(max($tmp_tmp_wins_list), $tmp_tmp_wins_list);
            $rank_list[$r_key] = $count;
            $count ++;
            unset($tmp_tmp_wins_list[$r_key]); 
        }
     }
    
    if($group_num==3) {
        foreach ($tmp_wins_list as $key => $value) {
            $row = array();
            $row['competition_id'] = $this->competition_id;
            $row['position'] = $this->clash_second_transform[$key];
            $row['user_id'] = $pos_user_id[$row['position']];
            $row['wins'] = $value;
            $row['net_wins'] = $tmp_net_wins_list[$key];
            $row['rank'] = $rank_list[$key];
            $row['g_item'] = $this->cur_item;
            //var_dump($row);
         
            if(!M('Competition_secondstep_group3_final_score')->add($row)) {

           }
         
       }
    }

     //var_dump($tran_wins_list);
    //var_dump($tmp_net_wins_list);
     //var_dump($rank_list);
     //exit();

    
    if($competition['com_model_type']==2){
       if($com_secondstep_record['clash_item1']==$this->cur_item) {
          $com_secondstep_record['progressing1'] = 2;
       }else if($com_secondstep_record['clash_item2']==$this->cur_item){
          $com_secondstep_record['progressing2'] = 2;
       }else if($com_secondstep_record['clash_item3']==$this->cur_item){
          $com_secondstep_record['progressing3'] = 2;
       }

       $scr_where = array();
       $scr_where['competition_id'] = $this->competition_id;
       if(!M('Com_secondstep_record')->where($scr_where)->save($com_secondstep_record)) {

       }

    } else if($competition['com_model_type']==1) {
       $s_scr_where = array();
       $s_scr_where['competition_id'] = $this->competition_id;
       $com_sin_secondstep_record['progressing'] =2;
       if(!M('Com_single_secondstep_record')->where($s_scr_where)->save($com_sin_secondstep_record)) {

       }
    }

    $second_out= array();
    switch ($group_num) {
         case '2':
             $second_out['c'] = array();
             $second_out['d'] = array();
             break;
         
         case '3':
             $second_out['d'] = array();
             $second_out['e'] = array();
             break;

         case '4':
             $second_out['e1'] = array();
             $second_out['e2'] = array();
             $second_out['f1'] = array();
             $second_out['f2'] = array();             
             break;

         case '6':
            $second_out['g1'] = array();
            $second_out['g2'] = array();
            $second_out['g3'] = array();
             break;

         case '8':
             $second_out['j1'] = array();
             $second_out['j2'] = array();
             $second_out['j3'] = array();
             $second_out['j4'] = array();
             break;

         default:
             # code...
             break;
     }

     foreach ($rank_list as $key => $value) {
        if($value == 1||$value == 2) {
            if($group_num==2||$group_num==3) {
                $second_out[substr($key,0,1)][$value] = $key;
            } else {
                $second_out[substr($key,0,2)][$value] = $key;
            }
        }
       
    }

        //var_dump($second_out);
        //var_dump($pos_user_id);

     switch ($group_num) {
         case 2:
             $row = array();
             $row['competition_id'] = $this->competition_id;
             $row['com_unit'] = $competition['competition_regist_unit'];
             $row['g_item'] = $this->cur_item; 
             $row['e_cs'] = $pos_user_id[$this->clash_second_transform[$second_out['c'][1]]];
             $row['e_ds'] = $pos_user_id[$this->clash_second_transform[$second_out['d'][1]]];
             $row['f_cf'] = $pos_user_id[$this->clash_second_transform[$second_out['c'][2]]];
             $row['f_df'] = $pos_user_id[$this->clash_second_transform[$second_out['d'][2]]];
             //var_dump($row);
             M('competition_secondstep_clash_'.$group_num.'group_list')->save($row);
             break;

         case 3:
             # code...
             break;

         case 4:
             $row = array();
             $row['competition_id'] = $this->competition_id;
             $row['com_unit'] = $competition['competition_regist_unit'];
             $row['g_item'] = $this->cur_item;
             $row['h1_e1'] = $pos_user_id[$this->clash_second_transform[$second_out['e1'][1]]];
             $row['h1_e2'] = $pos_user_id[$this->clash_second_transform[$second_out['e2'][1]]];
             $row['h2_f1'] = $pos_user_id[$this->clash_second_transform[$second_out['f1'][1]]];
             $row['h2_f2'] = $pos_user_id[$this->clash_second_transform[$second_out['f2'][1]]];
             $row['j1_e3'] = $pos_user_id[$this->clash_second_transform[$second_out['e1'][2]]];
             $row['j1_e2'] = $pos_user_id[$this->clash_second_transform[$second_out['e2'][2]]];
             $row['j2_f1'] = $pos_user_id[$this->clash_second_transform[$second_out['f1'][2]]];
             $row['j2_f2'] = $pos_user_id[$this->clash_second_transform[$second_out['f2'][2]]];

             M('Competition_secondstep_clash_'.$group_num.'group_list')->save($row);
             break;

         case 6:
             $row = array();
             $row['competition_id'] = $this->competition_id;
             $row['com_unit'] = $competition['competition_regist_unit'];
             $row['g_item'] = $this->cur_item;
             $row['gs_g1'] = $pos_user_id[$this->clash_second_transform[$second_out['g1'][1]]];
             $row['gs_g2'] = $pos_user_id[$this->clash_second_transform[$second_out['g2'][1]]];
             $row['gs_g3'] = $pos_user_id[$this->clash_second_transform[$second_out['g3'][1]]];
             $row['gf_g4'] = $pos_user_id[$this->clash_second_transform[$second_out['g1'][2]]];
             $row['gf_g5'] = $pos_user_id[$this->clash_second_transform[$second_out['g2'][2]]];
             $row['gf_g6'] = $pos_user_id[$this->clash_second_transform[$second_out['g3'][2]]];             
             M('competition_secondstep_clash_'.$group_num.'group_list')->save($row);
             break;

         case 8:
             $row = array();
             $row['competition_id'] = $this->competition_id;
             $row['com_unit'] = $competition['competition_regist_unit'];
             $row['g_item'] = $this->cur_item;
             $row['k1_j1'] = $pos_user_id[$this->clash_second_transform[$second_out['j1'][1]]];
             $row['k1_j2'] = $pos_user_id[$this->clash_second_transform[$second_out['j2'][1]]];
             $row['k2_j3'] = $pos_user_id[$this->clash_second_transform[$second_out['j3'][1]]];
             $row['k2_j4'] = $pos_user_id[$this->clash_second_transform[$second_out['j4'][1]]];
             $row['l1_j1'] = $pos_user_id[$this->clash_second_transform[$second_out['j1'][2]]];
             $row['l1_j2'] = $pos_user_id[$this->clash_second_transform[$second_out['j2'][2]]];
             $row['l2_j3'] = $pos_user_id[$this->clash_second_transform[$second_out['j3'][2]]];
             $row['l2_j4'] = $pos_user_id[$this->clash_second_transform[$second_out['j4'][2]]];
             M('competition_secondstep_clash_'.$group_num.'group_list')->save($row);
             break;
         
         default:
             # code...
             break;
     }

     $this->success('保存成功！',U('Competition/create_competition_clash_result',array('competition_id'=>$this->competition_id,'select_item'=>0)),5);
    
   }

    
   public function score_count_to_second_three() {

     $this->competition_id = I('get.competition_id');
     $this->select_item = I('get.select_item');
     $this->cur_item = I('get.cur_item');

     $com_where['competition_id'] = $this->competition_id;
     $competition = M('Competition_table')->where($com_where)->find();
 
     //读取分组数
     $group_num = $this->group_num_transform[$competition['competition_regist_unit']];

     if($group_num == 0||$group_num == 2||$group_num == 3) {
        $this->success('竞赛已结束！',U('Competition/create_competition_clash_result',array('competition_id'=>$this->competition_id,'select_item'=>0)),5);
        return ;
     }
     
     //用于读取竞赛记录状况
     $com_secondstep_record=array();
     $com_sin_secondstep_record=array();
     
     //判断比赛类型
     if($competition['com_model_type']==2) {

       //读取比赛情况记录 
       $scr_where1 = array();
       $scr_where1['competition_id'] = $this->competition_id;
       $com_secondstep_record = M('Com_secondstep_record')->where($scr_where1)->find();
       if($com_secondstep_record['clash_item1']==$this->cur_item) {
            if($com_secondstep_record['progressing1'] >= 3) {
                $this->success('竞赛已进入第二阶段（第三轮），不可重复操作！',U('Competition/create_competition_clash_result',array('competition_id'=>$this->competition_id,'select_item'=>0)),5);
                return ;
            } 
         }else if($com_secondstep_record['clash_item2']==$this->cur_item){
           if($com_secondstep_record['progressing2'] >= 3) {
                $this->success('竞赛已进入第二阶段（第三轮），不可重复操作！',U('Competition/create_competition_clash_result',array('competition_id'=>$this->competition_id,'select_item'=>0)),5);
                return ;
            }
         }else if($com_secondstep_record['clash_item3']==$this->cur_item){
             if($com_secondstep_record['progressing3'] >= 3) {
                $this->success('竞赛已进入第二阶段（第三轮），不可重复操作！',U('Competition/create_competition_clash_result',array('competition_id'=>$this->competition_id,'select_item'=>0)),5);
                return ;
            }
         }
    } else if($competition['com_model_type']==1) {
       //读取比赛情况记录 
       $s_scr_where = array();
       $s_scr_where['competition_id'] = $this->competition_id;
       $com_sin_secondstep_record = M('Com_single_secondstep_record')->where($s_scr_where)->find();
       
       if($com_sin_secondstep_record['progressing'] >= 3) {
          $this->success('竞赛已进入第二阶段（第三轮），不可重复操作！',U('Competition/create_competition_clash_result',array('competition_id'=>$this->competition_id,'select_item'=>0)),5);
          return ;
       }
    }

     $sc_where = array();
     $sc_where['Competition_id'] = $this->competition_id;
     $sc_where['g_item'] = $this->cur_item;
     $second_score = M('Competition_secondstep_clash_score')->where($sc_where)->select();
     
    //var_dump($second_score);
     $tran_second_score = array();
     foreach ($second_score as $key => $value) {
        if(array_search($value['mode'],$this->second_second_clash)!=false&&array_search($value['mode'],$this->second_second_clash)!=null) {
            $tran_second_score[$this->tr_score_second_mode_transform[$value['mode']]] = $value['score'];
        }
        
     }

     //var_dump($tran_second_score);

    
     
     $scl_where=array();
     $scl_where['competition_id']= $this->competition_id;
     $scl_where['g_item'] = $this->cur_item;
     $sec_clash_list = M('Competition_secondstep_clash_'.$group_num.'group_list')->where($scl_where)->select();

     //var_dump($sec_clash_list);
     $com_user_pos_list = array();

     //构造参赛人员与对应位置数组
     foreach ($sec_clash_list[0] as $key => $value) {
              if($key!='competition_id'&&$key!='com_unit'&&$key!='g_item'&&$value!=null) {
                if(array_search($key,$this->second_second_position)!=false&&array_search($key, $this->second_second_position)!=null) {
                  $row = array();
                  $row['user_id'] = $value;
                  $row['competition_id'] = $this->competition_id;
                  $row['item'] = $this->cur_item;
                  $row['position'] = $this->clash_second_transform[$key];
                  //var_dump($row);
                  array_push($com_user_pos_list,$row);
                }
              }
          }

     //var_dump($com_user_pos_list);
     
     $tmp_wins_list=array();
     $tmp_losts_list=array();
     $tmp_net_wins_list=array();
     $pos_user_id = array();
     $rank_list=array();
     $score_rank_list = array();
    
    foreach ($com_user_pos_list as $key => $value) {
         $pos_user_id[$value['position']] = $value['user_id'];
         $tmp_wins_list[$this->tr_clash_second_transform[$value['position']]]=0;
         $tmp_losts_list[$this->tr_clash_second_transform[$value['position']]]=0;
         $rank_list[$this->tr_clash_second_transform[$value['position']]]=0;
         $score_rank_list[$this->tr_clash_second_transform[$value['position']]]=0;
     }

      //var_dump($pos_user_id);
      //echo "11111111111111111111111111111";

    foreach ($tran_second_score as $key => $value) {
         $strs = explode('_', $key);
         //var_dump($strs);        
         $pre_pos = $strs[1]."_".$strs[2];
         $post_pos = $strs[3]."_".$strs[4];       
         $tmp_wins_list[$pre_pos] += $value;
         $tmp_losts_list[$post_pos] += $value;
     }

     //var_dump($tmp_wins_list);
     //var_dump($tmp_losts_list);
     $tran_wins_list = array();
     switch ($group_num) {
         case '2':
             #code...
             break;
         
         case '3':
             #code...
             break;

         case '4':
             $tran_wins_list['h1'] = array();
             $tran_wins_list['h2'] = array();
             $tran_wins_list['j1'] = array();
             $tran_wins_list['j2'] = array();             
             break;

         case '6':
             $tran_wins_list['gs'] = array();
             $tran_wins_list['gf'] = array();
             break;

         case '8':
             $tran_wins_list['k1'] = array();
             $tran_wins_list['k2'] = array();
             $tran_wins_list['l1'] = array();
             $tran_wins_list['l2'] = array();
             break;

         default:
             # code...
             break;
     }

     foreach ($tmp_wins_list as $key => $value) {
         $tmp_net_wins_list[$key] = $value - $tmp_losts_list[$key];
         $score_rank_list[$key] = $value*100+$tmp_net_wins_list[$key]+20;
         $tran_wins_list[substr($key,0,2)][$key] = $score_rank_list[$key];      
     }

     //var_dump($tran_wins_list);

     $tmp_tran_wins_list = $tran_wins_list;
     foreach ($tmp_tran_wins_list as $key => $value) {
         $tmp_tmp_wins_list = $value;
         $count =1;
         while (sizeof($tmp_tmp_wins_list)!=0) {
            $r_key = array_search(max($tmp_tmp_wins_list), $tmp_tmp_wins_list);
            $rank_list[$r_key] = $count;
            $count ++;
            unset($tmp_tmp_wins_list[$r_key]); 
        }
     }
    
     if($group_num==6) {
        foreach ($tmp_wins_list as $key => $value) {
            $row = array();
            $row['competition_id'] = $this->competition_id;
            $row['position'] = $this->clash_second_transform[$key];
            $row['user_id'] = $pos_user_id[$row['position']];
            $row['wins'] = $value;
            $row['net_wins'] = $tmp_net_wins_list[$key];
            $row['rank'] = $rank_list[$key];
            $row['g_item'] = $this->cur_item;
            //var_dump($row);
         
            if(!M('Competition_secondstep_group6_final_score')->add($row)) {

           }
         
       }
    }

      // var_dump($tran_wins_list);
      // var_dump($tmp_net_wins_list);
      // var_dump($rank_list);
      // exit();

    
    if($competition['com_model_type']==2){
       if($com_secondstep_record['clash_item1']==$this->cur_item) {
          $com_secondstep_record['progressing1'] = 3;
       }else if($com_secondstep_record['clash_item2']==$this->cur_item){
          $com_secondstep_record['progressing2'] = 3;
       }else if($com_secondstep_record['clash_item3']==$this->cur_item){
          $com_secondstep_record['progressing3'] = 3;
       }

       $scr_where = array();
       $scr_where['competition_id'] = $this->competition_id;
       if(!M('Com_secondstep_record')->where($scr_where)->save($com_secondstep_record)) {

       }

    } else if($competition['com_model_type']==1) {
       $s_scr_where = array();
       $s_scr_where['competition_id'] = $this->competition_id;
       $com_sin_secondstep_record['progressing'] =3;
       if(!M('Com_single_secondstep_record')->where($s_scr_where)->save($com_sin_secondstep_record)) {

       }
    }

    $second_out= array();
    switch ($group_num) {
         case '2':
             #code...
             break;
         
         case '3':
             #code...
             break;

         case '4':
             $second_out['h1'] = array();
             $second_out['h2'] = array();
             $second_out['j1'] = array();
             $second_out['j2'] = array();             
             break;

         case '6':
            #code...
             break;

         case '8':
             $second_out['k1'] = array();
             $second_out['k2'] = array();
             $second_out['l1'] = array();
             $second_out['l2'] = array();
             break;

         default:
             # code...
             break;
     }

     foreach ($rank_list as $key => $value) {
        if($value == 1||$value == 2) {
            $second_out[substr($key,0,2)][$value] = $key;        
        }
       
    }

        //var_dump($second_out);
        //var_dump($pos_user_id);

     switch ($group_num) {
         case 2:
             # code...
             break;

         case 3:
             # code...
             break;

         case 4:
             $row = array();
             $row['competition_id'] = $this->competition_id;
             $row['com_unit'] = $competition['competition_regist_unit'];
             $row['g_item'] = $this->cur_item;
             $row['h1s'] = $pos_user_id[$this->clash_second_transform[$second_out['h1'][1]]];
             $row['h2s'] = $pos_user_id[$this->clash_second_transform[$second_out['h2'][1]]];
             $row['h1f'] = $pos_user_id[$this->clash_second_transform[$second_out['h1'][2]]];
             $row['h2f'] = $pos_user_id[$this->clash_second_transform[$second_out['h2'][2]]];
             $row['j1s'] = $pos_user_id[$this->clash_second_transform[$second_out['j1'][1]]];
             $row['j2s'] = $pos_user_id[$this->clash_second_transform[$second_out['j2'][1]]];
             $row['j1f'] = $pos_user_id[$this->clash_second_transform[$second_out['j1'][2]]];
             $row['j2f'] = $pos_user_id[$this->clash_second_transform[$second_out['j2'][2]]];

             M('Competition_secondstep_clash_'.$group_num.'group_list')->save($row);
             break;

         case 6:
             #code...
             break;

         case 8:
             $row = array();
             $row['competition_id'] = $this->competition_id;
             $row['com_unit'] = $competition['competition_regist_unit'];
             $row['g_item'] = $this->cur_item;
             $row['k1s'] = $pos_user_id[$this->clash_second_transform[$second_out['k1'][1]]];
             $row['k2s'] = $pos_user_id[$this->clash_second_transform[$second_out['k2'][1]]];
             $row['k1f'] = $pos_user_id[$this->clash_second_transform[$second_out['k1'][2]]];
             $row['k2f'] = $pos_user_id[$this->clash_second_transform[$second_out['k2'][2]]];
             $row['l1s'] = $pos_user_id[$this->clash_second_transform[$second_out['l1'][1]]];
             $row['l2s'] = $pos_user_id[$this->clash_second_transform[$second_out['l2'][1]]];
             $row['l1f'] = $pos_user_id[$this->clash_second_transform[$second_out['l1'][2]]];
             $row['l2f'] = $pos_user_id[$this->clash_second_transform[$second_out['l2'][2]]];
             M('competition_secondstep_clash_'.$group_num.'group_list')->save($row);
             break;
         
         default:
             # code...
             break;
     }

     $this->success('保存成功！',U('Competition/create_competition_clash_result',array('competition_id'=>$this->competition_id,'select_item'=>0)),5);
    
   }


   //保存成绩填写
   public function save_competition_result() {
      $data=I('post.');
      //var_dump($data);
      //exit();
      $this->mod_item = I('post.mod_item');
      $this->mod_s_item = I('post.mod_s_item');

      //判断记录哪个阶段的成绩
      if($this->mod_item==2) {
        $this->competition_id = $data['competition_id'];
        $this->g_item = $data['g_item'];
        $score_data=array();
        foreach ($data as $key => $value) {
            if($key!='g_item'&&$key!='competition_id'&&$key!='mod_item'&&$key!='mod_s_item') {
              $score_item=array();
              $score_item['competition_id'] = $this->competition_id;
              $score_item['mode'] = $this->score_mode_transform[$key];
              $score_item['score'] = $value;
              $score_item['g_item'] = $this->g_item;
              array_push($score_data,$score_item);
              if(!M('Competition_firststep_clash_score')->add($score_item)) {
                $where=array();
                $where['competition_id'] = $this->competition_id;
                $where['mode'] = $score_item['mode'];
                $where['g_item'] = $score_item['g_item'];
                $ans = M('Competition_firststep_clash_score')->where($where)->save($score_item);
              //echo $ans."<br />"; 
              }          
            }
        }
      } else if($this->mod_s_item==2) {
        $this->competition_id = $data['competition_id'];
        $this->g_item = $data['g_item'];
        $score_data=array();
        foreach ($data as $key => $value) {
            if($key!='g_item'&&$key!='competition_id'&&$key!='mod_item'&&$key!='mod_s_item') {
              $score_item=array();
              $score_item['competition_id'] = $this->competition_id;
              $score_item['mode'] = $this->score_second_mode_transform[$key];
              $score_item['score'] = $value;
              $score_item['g_item'] = $this->g_item;
              array_push($score_data,$score_item);
              if(!M('Competition_secondstep_clash_score')->add($score_item)) {
                $where=array();
                $where['competition_id'] = $this->competition_id;
                $where['mode'] = $score_item['mode'];
                $where['g_item'] = $score_item['g_item'];
                $ans = M('Competition_secondstep_clash_score')->where($where)->save($score_item);
              //echo $ans."<br />"; 
              }          
            }
        }
        //var_dump($score_data);
        //exit();
      }
      
      $this->success('保存成功！',U('Competition/create_competition_clash_result',array('competition_id'=>$this->competition_id,'select_item'=>0)),5);
      //$score_list=array();
      //echo $this->competition_id."<br>".$this->g_item;
      //var_dump($score_data);

   }

    /**********比赛展示给用户部分**********/

    /**
     * 加载比赛展示页面
     */
    public function index(){
        $this->display();
    }

    /**
     * 按时间列表当前市下的比赛
     */
    public function listCompetitionByTime(){
        //从前端获取地理信息
        $competitionLocation['competition_province'] = $_POST['competition_province'];
        $competitionLocation['competition_city'] = $_POST['competition_city'];

        //实例化比赛对象
        $competition = M('competition_table');

        //位置信息
        $e_page_num = $this->list_each_page_number;  //每一页的球馆条数
        $page_num = $_POST['page_num'];;  //页数

        //置位
        $start_p = $e_page_num * ($page_num - 1);

        //查询比赛
        $where['competition_province'] = $competitionLocation['competition_province'];
        $where['competition_city'] = $competitionLocation['competition_city'];
        $where['competition_public'] = 1;
        $result = $competition->where($where)->field('competition_id, competition_name, competition_publish_time, competition_begin_time, competition_picture, competition_status, com_model_item, competition_regist_unit, competition_contain_number')->limit($start_p, $e_page_num)->select();
        $resultCount = $competition->where($where)->count();

        //为每个竞赛加入报名项目参数
        $madeResult = array();
        for($x1 = 0; $x1 < count($result); $x1++){
            $competitionInfo = $result[$x1];
            $item_list=array();
            if($competitionInfo['com_model_item']<=5) {
                if($competitionInfo['competition_regist_unit']<$competitionInfo['competition_contain_number']) {
                   array_push($item_list, intval($competitionInfo['com_model_item']));
                }
            } else {
                $c_where = array();
                $c_where['competition_id'] = $competitionInfo['competition_id'];
                $item_count = M('Competition_user')->where($c_where)->field('item,count(item)')->group('item')->select();
                $tran_item_count = array();
                foreach ($item_count as $key => $value) {
                    $tran_item_count[$value['item']] = $value['count(item)'];
                }
                switch ($competitionInfo['com_model_item']) {
                    case '6':
                        if($tran_item_count['3']==null||$tran_item_count['3'] < $competitionInfo['competition_regist_unit']) {
                           array_push($item_list, 3);
                        }
                        if($tran_item_count['4']==null||$tran_item_count['4'] < $competitionInfo['competition_regist_unit']) {
                           array_push($item_list, 4);
                        }
                        if($tran_item_count['5']==null||$tran_item_count['5'] < $competitionInfo['competition_regist_unit']) {
                           array_push($item_list, 5);
                        }
                        break;

                    case '7':
                        if($tran_item_count['3']==null||$tran_item_count['3'] < $competitionInfo['competition_regist_unit']) {
                           array_push($item_list, 3);
                        }
                        if($tran_item_count['5']==null||$tran_item_count['4'] < $competitionInfo['competition_regist_unit']) {
                           array_push($item_list, 5);
                        }
                        if($tran_item_count['6']==null||$tran_item_count['5'] < $competitionInfo['competition_regist_unit']) {
                           array_push($item_list, 6);
                        }
                        break;

                    case '8':
                        if($tran_item_count['1']==null||$tran_item_count['3'] < $competitionInfo['competition_regist_unit']) {
                           array_push($item_list, 1);
                        }
                        if($tran_item_count['3']==null||$tran_item_count['4'] < $competitionInfo['competition_regist_unit']) {
                           array_push($item_list, 3);
                        }
                        if($tran_item_count['5']==null||$tran_item_count['5'] < $competitionInfo['competition_regist_unit']) {
                           array_push($item_list, 5);
                        }
                        break;
                    
                    default:
                        # code...
                        break;
                }
            }

            array_push($competitionInfo, $item_list);



            $madeResult[$x1] = $competitionInfo;
        }
        
        //ajax返回
        $resultList['result'] = $madeResult;
        $resultList['page_num'] = $page_num;
        $resultList['competition_count'] = $resultCount;
        $this->ajaxReturn($resultList);
    }

    /**
     * 比赛搜索
     */
    public function competitionSearch(){
        //从前端获取模糊信息
        $competitionName = $_POST['competition_name'];

        //实例化比赛对象
        $competition = M('competition_table');

        //位置信息
        $e_page_num = $this->list_each_page_number;  //每一页的球馆条数
        $page_num = $_POST['page_num'];;  //页数

        //置位
        $start_p = $e_page_num * ($page_num - 1);

        //查询比赛
        $name = "%".$competitionName;
        $where['competition_name'] = array('like', $competitionName."%");
        $result = $competition->where($where)->field('competition_id, competition_name, competition_publish_time, competition_begin_time, competition_picture, competition_status, com_model_item, competition_regist_unit, competition_contain_number')->limit($start_p, $e_page_num)->select();
        $resultCount = $competition->where($where)->count();

        //为每个竞赛加入报名项目参数
        $madeResult = array();
        for($x1 = 0; $x1 < count($result); $x1++){
            $competitionInfo = $result[$x1];






            $madeResult[$x1] = $competitionInfo;
        }

        //ajax返回
        $resultList['result'] = $madeResult;
        $resultList['page_num'] = $page_num;
        $resultList['competition_count'] = $resultCount;
        $this->ajaxReturn($resultList);
    }

    /**
     * 进入比赛详情页面
     */
    public function competitionInfo(){
        //从前端获取比赛id
        $competitionId = $_POST['competition_id'];

        //实例化比赛对象
        $competition = M('competition_table');
        //实例化球馆对象
        $gymnasium = M('gymnasium_table');
        //实例化用户对象
        $user = M('user_table');

        $resultList = array();

        //查询比赛信息
        $where['competition_id'] = $competitionId;
        $competitionResult = $competition->where($where)->find();
        $resultList['competitionResult'] = $competitionResult;
        //查询比赛球馆信息
        unset($where);
        $where['gymnasium_id'] = $competitionResult['competition_gymnasium'];
        $gymnasiumResult = $gymnasium->where($where)->field('gymnasium_id, gymnasium_name')->find();
        $resultList['gymnasiumResult'] = $gymnasiumResult;
        //查询比赛组织者信息
        unset($where);
        $where['id'] = $competitionResult['competition_organizer_id'];
        $userResult = $user->where($where)->field('id, name')->find();
        $resultList['userResult'] = $userResult;

        $this->ajaxReturn($resultList);
    }

    /**
     * 竞赛报名
     * ajax返回：
     *     0：已截止报名
     *     1：报名成功
     *     2：已经报过名了
     */
    public function competitionApply(){
        //从前端获取比赛id
        $competitionId = $_POST['competition_id'];

        //实例化比赛对象
        $competition = M('competition_table');
        //实例化比赛报名对象
        $competitionApply = M('competition_apply_user_table');

        //比照报名时间
        $where['competition_id'] = $competitionId;
        $competitionInfo = $competition->where($where)->field('competition_deadline_registration')->find();
        $storeTimestamp = strtotime($competitionInfo['competition_deadline_registration']);
        if($storeTimestamp < time()){
            $result = 0;
        }else{
            $data['competition_id'] = $competitionId;
            $data['apply_user_id'] = session('id');
            $data['apply_date'] = date("y-m-d H:i:s");
            $addResult = $competitionApply->add($data);
            if($addResult){
                $result = 1;
            }else{
                $result = 2;
            }
        }

        $this->ajaxReturn($result);
    }

    
}